/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import javax.swing.JOptionPane;
import model.dao.DaoAdm;
import model.dominio.Adm;

/**
 *
 * @author Teao
 */
public class GuiAdmCadastrar extends javax.swing.JPanel {

    /**
     * Creates new form GuiAdmCadastrar
     */
    public GuiAdmCadastrar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        nome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cpf = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        senha = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jTextField1.setText("jTextField1");

        setLayout(null);

        jLabel1.setText("Nome:");
        add(jLabel1);
        jLabel1.setBounds(20, 30, 50, 17);
        add(nome);
        nome.setBounds(70, 20, 380, 30);

        jLabel2.setText("CPF:");
        add(jLabel2);
        jLabel2.setBounds(20, 70, 31, 17);

        try {
            cpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfActionPerformed(evt);
            }
        });
        add(cpf);
        cpf.setBounds(70, 60, 170, 30);

        jButton1.setText("Cancelar");
        add(jButton1);
        jButton1.setBounds(280, 210, 160, 50);

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(30, 210, 160, 50);

        senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                senhaActionPerformed(evt);
            }
        });
        add(senha);
        senha.setBounds(70, 100, 170, 30);

        jLabel3.setText("Senha");
        add(jLabel3);
        jLabel3.setBounds(10, 110, 43, 17);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Adm admin = new Adm();
        if (nome.getText().isEmpty() || cpf.getText().isEmpty() || senha.getText().isEmpty()){
            JOptionPane.showMessageDialog(this,"Digite os dados corretamente","ERRO", JOptionPane.ERROR_MESSAGE);
        }else{
            admin.setNome(nome.getText());
            admin.setCpf(cpf.getText());
            admin.setSenha(senha.getText());
            DaoAdm dao = new DaoAdm();
            admin = (Adm) dao.salvar(admin);
            if(admin != null){
                JOptionPane.showMessageDialog(this,"Dados registrados com sucesso","Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cpfActionPerformed

    private void senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_senhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_senhaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField cpf;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField nome;
    private javax.swing.JTextField senha;
    // End of variables declaration//GEN-END:variables
}
